package com.kingint.web.listener.rushHouse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kingint.mbg.ser.entity.SerWorkOrder;
import com.kingint.mbg.ser.mapper.SerWorkOrderMapper;
import com.kingint.web.util.spring.SpringContextHolder;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;

import java.util.Date;

public class RushReceiveListener implements TaskListener {


    @Override
    public void notify(DelegateTask delegateTask) {
        SerWorkOrderMapper orderMapper = SpringContextHolder.getBean(SerWorkOrderMapper.class);
        String processInstanceId = delegateTask.getProcessInstanceId();//流程实例id
        String eventName = delegateTask.getEventName();//获取事件名称
        if (eventName.equals("create")) {
            QueryWrapper<SerWorkOrder> wrapper = new QueryWrapper<>();
            wrapper.eq("process_instance_id", processInstanceId);
            SerWorkOrder workOrder = new SerWorkOrder();
            workOrder.setOrderStatus(2);//处理中
            workOrder.setStartTime(new Date());//开始处理时间
            orderMapper.update(workOrder, wrapper);//更新相关额状态
        }
        if (eventName.equals("complete")) {//完成
            String haveCheckStatus = delegateTask.getVariable("haveCheckStatus").toString();
            if(haveCheckStatus!=null&&haveCheckStatus.equals("0")){
                QueryWrapper<SerWorkOrder> wrapper = new QueryWrapper<>();
                wrapper.eq("process_instance_id", processInstanceId);
                SerWorkOrder workOrder = new SerWorkOrder();
                workOrder.setOrderStatus(5);//完成，验收
                workOrder.setEndTime(new Date());//完成时间
                orderMapper.update(workOrder, wrapper);//更新相关额状态
            }
        }
    }
}
